package models

import (
	"github.com/astaxie/beego/orm"
	"h1/utils"
)

// 批量插入场景-图片数据库表
func MultiInsertSceneRelateImage(sceneId int64, imageArr []int64) ([]map[string]interface{}, error) {
	// 创建字典数组
	resTree := utils.MakeDicArray()
	// 创建数组对象
	var multiScene []SceneRelateImage
	// 场景模型
	sceneOrg := ImageResource{}
	// 去重复
	Duplice := utils.Duplicate(imageArr)

	for _, item := range Duplice {
		var resultStr, code = "", 0
		qarr, _ := MutiTableQuery("*", "tb_scene_relate_image", "file_id="+utils.Int64ToString(item.(int64))+" and scene_id="+utils.Int64ToString(sceneId))
		_, serr := sceneOrg.GetFileById(item.(int64))
		currentModel := SceneRelateImage{}
		currentModel.SceneId = sceneId
		currentModel.FileId = item.(int64)
		//建立个json
		res := utils.MakeJson()
		if len(qarr) == 0 && serr == nil {
			multiScene = append(multiScene, currentModel)
			resultStr = "关联成功"
			code = 0
		} else {
			code = 1
			if serr != nil {
				resultStr = "在图片文件表中不存在"
			}
			if len(qarr) != 0 {
				resultStr = "已经关联，请勿重复关联！"
			}
		}
		//res := ScentImageResourceJson(&currentModel)
		res["file_id"] = item.(int64)
		res["scene_id"] = sceneId
		res["msg"] = resultStr
		res["code"] = code
		resTree = append(resTree, res)
	}
	_, err := orm.NewOrm().InsertMulti(len(multiScene), multiScene)
	return resTree, err
}

// 批量插入场景-视频数据库表
func MultiInsertSceneRelateVideo(sceneId int64, videoArr []int64) ([]map[string]interface{}, error) {
	// 创建字典数组
	resTree := utils.MakeDicArray()
	// 创建数组对象
	var multiScene []SceneRelateVideo
	// 场景模型
	sceneOrg := VideoResource{}
	// 去重复
	Duplice := utils.Duplicate(videoArr)

	for _, item := range Duplice {
		var resultStr, code = "", 0
		qarr, _ := MutiTableQuery("*", "tb_scene_relate_video", "file_id = "+utils.Int64ToString(item.(int64))+" and scene_id="+utils.Int64ToString(sceneId))
		_, serr := sceneOrg.GetFileById(item.(int64))
		currentModel := SceneRelateVideo{}
		currentModel.SceneId = sceneId
		currentModel.FileId = item.(int64)
		//建立个json
		res := utils.MakeJson()
		if len(qarr) == 0 && serr == nil {
			multiScene = append(multiScene, currentModel)
			resultStr = "关联成功"
			code = 0
		} else {
			code = 1
			if serr != nil {
				resultStr = "在视频文件表中不存在"
			}
			if len(qarr) != 0 {
				resultStr = "已经关联，请勿重复关联！"
			}
		}
		//res := ScentSceneResourceJson(&currentModel)
		res["msg"] = resultStr
		res["code"] = code
		res["scene_id"] = sceneId
		res["file_id"] = item.(int64)
		resTree = append(resTree, res)
	}
	_, err := orm.NewOrm().InsertMulti(len(multiScene), multiScene)
	return resTree, err
}

// 批量插入场景-场景数据库表
func MultiInsertSceneRelateScene(sceneId int64, sceneArr []int64) ([]map[string]interface{}, error) {
	// 创建字典数组
	resTree := utils.MakeDicArray()
	// 创建数组对象
	var multiScene []SceneRelateScene
	// 场景模型
	sceneOrg := Scene{}
	// 去重复
	Duplice := utils.Duplicate(sceneArr)
	for _, item := range Duplice {
		var resultStr, code = "", 0
		qarr, _ := MutiTableQuery("*", "tb_scene_relate_scene", "relate_scene_id="+utils.Int64ToString(item.(int64))+" and scene_id="+utils.Int64ToString(sceneId))
		_, serr := sceneOrg.GetInfoById(item.(int64))
		currentModel := SceneRelateScene{}
		currentModel.SceneId = sceneId
		currentModel.RelateSceneId = item.(int64)
		//建立个json
		res := utils.MakeJson()
		if len(qarr) == 0 && serr == nil {
			multiScene = append(multiScene, currentModel)
			resultStr = "关联成功"
			code = 0
		} else {
			code = 1
			if serr != nil {
				resultStr = "在场景表中不存在"
			}
			if len(qarr) != 0 {
				resultStr = "已经关联，请勿重复关联！"
			}
		}
		//res := ScentSceneResourceJson(&currentModel)
		res["msg"] = resultStr
		res["code"] = code
		res["scene_id"] = sceneId
		res["relate_scene_id"] = item.(int64)
		resTree = append(resTree, res)
	}
	_, err := orm.NewOrm().InsertMulti(len(multiScene), multiScene)
	return resTree, err
}

// 批量插入场景-元素数据库表
func MultiInsertSceneRelateElement(sceneId int64, elementArr []int64) ([]map[string]interface{}, error) {
	// 创建字典数组
	resTree := utils.MakeDicArray()
	// 创建数组对象
	var multiScene []SceneRelateElement
	// 场景模型
	sceneOrg := Element{}
	// 去重复
	Duplice := utils.Duplicate(elementArr)
	for _, item := range Duplice {
		var resultStr, code = "", 0
		qarr, _ := MutiTableQuery("*", "tb_scene_relate_element", "element_id="+utils.Int64ToString(item.(int64))+" and scene_id="+utils.Int64ToString(sceneId))
		_, serr := sceneOrg.GetElementById(item.(int64))
		currentModel := SceneRelateElement{}
		currentModel.SceneId = sceneId
		currentModel.ElementId = item.(int64)
		//建立个json
		res := utils.MakeJson()
		if len(qarr) == 0 && serr == nil {
			multiScene = append(multiScene, currentModel)
			resultStr = "关联成功"
			code = 0
		} else {
			code = 1
			if serr != nil {
				resultStr = "在元素表中不存在"
			}
			if len(qarr) != 0 {
				resultStr = "已经关联，请勿重复关联！"
			}
		}
		res["msg"] = resultStr
		res["code"] = code
		res["scene_id"] = sceneId
		res["element_id"] = item.(int64)
		resTree = append(resTree, res)
	}
	_, err := orm.NewOrm().InsertMulti(len(multiScene), multiScene)
	return resTree, err
}

// 给项目批量插入场景
func MultiInsertProjectRelateScene(projectId int64, sceneArr []int64) ([]map[string]interface{}, error) {
	// 创建字典数组
	resTree := utils.MakeDicArray()
	// 创建数组对象
	var multiScene []ProjectScene
	// 场景模型
	sceneOrg := Scene{}
	// 去重复
	Duplice := utils.Duplicate(sceneArr)
	for _, item := range Duplice {
		var resultStr, code = "", 0
		qarr, _ := MutiTableQuery("*", "tb_project_scene", "scene_id="+utils.Int64ToString(item.(int64))+" and project_id="+utils.Int64ToString(projectId))
		_, serr := sceneOrg.GetInfoById(item.(int64))
		currentModel := ProjectScene{}
		currentModel.ProjectId = projectId
		currentModel.SceneId = item.(int64)
		//建立个json
		res := utils.MakeJson()
		if len(qarr) == 0 && serr == nil {
			multiScene = append(multiScene, currentModel)
			resultStr = "关联成功"
			code = 0
		} else {
			code = 1
			if serr != nil {
				resultStr = "在场景表中不存在"
			}
			if len(qarr) != 0 {
				resultStr = "已经关联，请勿重复关联！"
			}
		}
		//res := ScentSceneResourceJson(&currentModel)
		res["msg"] = resultStr
		res["code"] = code
		res["scene_id"] = item.(int64)
		res["project_id"] = projectId
		resTree = append(resTree, res)
	}
	_, err := orm.NewOrm().InsertMulti(len(multiScene), multiScene)
	return resTree, err
}

// 给项目批量关联文件
func MultiInsertSceneProjectRelateFiles(sceneId int64, fileArr []int64) ([]map[string]interface{}, error) {
	// 创建字典数组
	resTree := utils.MakeDicArray()
	// 创建数组对象
	var multiScene []ProjectFile
	// 场景模型
	sceneOrg := FileResource{}
	// 去重复
	Duplice := utils.Duplicate(fileArr)

	for _, item := range Duplice {
		var resultStr, code = "", 0
		qarr, _ := MutiTableQuery("*", "tb_project_file", "file_id="+utils.Int64ToString(item.(int64))+" and project_id="+utils.Int64ToString(sceneId))
		_, serr := sceneOrg.GetFileById(item.(int64))
		currentModel := ProjectFile{}
		currentModel.ProjectId = sceneId
		currentModel.FileId = item.(int64)
		//建立个json
		res := utils.MakeJson()
		if len(qarr) == 0 && serr == nil {
			multiScene = append(multiScene, currentModel)
			resultStr = "关联成功"
			code = 0
		} else {
			code = 1
			if serr != nil {
				resultStr = "文件表中不存在"
			}
			if len(qarr) != 0 {
				resultStr = "已经关联，请勿重复关联！"
			}
		}
		//res := ScentImageResourceJson(&currentModel)
		res["file_id"] = item.(int64)
		res["project_id"] = sceneId
		res["msg"] = resultStr
		res["code"] = code
		resTree = append(resTree, res)
	}
	_, err := orm.NewOrm().InsertMulti(len(multiScene), multiScene)
	return resTree, err
}
